package com.sservice.web.controller.admin;

import com.sservice.common.CompareObject;
import com.sservice.common.SameUrlData;
import com.sservice.urp.vo.DepVo;
import com.sservice.urp.vo.UserVo;
import com.sservice.service.service.IDepService;
import com.sservice.service.service.ISysLogService;
import com.sservice.service.service.IUserService;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.urp.entity.Dep;
import com.sservice.web.log.Action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/dep")
public class DepController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IDepService depService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ISysLogService sysLogService;

    /**
     * 创建
     *
     * @return
     */
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Object> create(Dep arg, HttpServletRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        if (arg.getComid() == null || arg.getName() == null) {
            return new BaseResult(false, "数据不完整!", 403);
        }
        Dep dep = depService.queryByDepNameAndComId(arg);
        if (dep != null) {
            return new BaseResult(false, "该部门名称已经存在", 403);
        }
        int service_res = depService.insertSelective(arg);
        if (service_res > 0) {
            sysLogService.log("创建", request, Integer.valueOf(arg.getId()), Action.depType);
        }
        BaseResult res = new BaseResult(true, service_res);
        return res;
    }

    /**
     * @param arg
     * @return
     */
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Object> delete(Dep arg, HttpServletRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        UserVo uv = new UserVo();
        BaseResult<Object> res;
        uv.setDepid(Integer.valueOf(arg.getId()));
        if (userService.queryCountByDepId(uv) > 0) {
            res = new BaseResult(false, "部门已关联用户，不能删除", 403);
        } else {

            int service_res = depService.deleteByPrimaryKey(arg.getId());
            if (service_res > 0) {
                sysLogService.log("删除", request, Integer.valueOf(arg.getId()), Action.depType);
            }
            res = new BaseResult(true, service_res);
        }
        return res;
    }

    /**
     * @param arg
     * @return
     */
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(Dep arg, HttpServletRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Dep dep = depService.selectByPrimaryKey(arg.getId());
        int service_res = depService.updateByPrimaryKeySelective(arg);
        if (service_res > 0) {
            Map<String, Object> res = CompareObject.contrastObj(arg, dep);
            sysLogService.log(res, request, Integer.valueOf(arg.getId()), Action.comType);
        }
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(DepVo arg) {
        List<Dep> list = depService.queryByPage(arg);
        int total = depService.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        System.out.println("Size:" + arg.getSize());
        System.out.println("getSortField:" + arg.getSortField());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
